<?php

require_once("classes/functions.php");
require_once("classes/security.class.php");

$action = getFromInput("a");
switch ($action){
	case "menadzerReport":
		menadzerReportExport();
		break;
	case "ulazUgovora":
		ulazUgovoraExport();
		break;
	case 'listing':
		listingDelatnostiXLS();
		break;
	case 'pretragaDel':
		pretragaDelatnostiXLS();
		break;
	case 'listingTXT':
		listingDelatnostiTXT();
		break;
	default:
		exportXLS();
		break;
}
function exportXLS(){
	$html = readValFromInput("html");
	$filename = "tmp".$_COOKIE["ypu"];
	
	//file_put_contents("tmpvoja",$html);
	
	$html = strip_tag_attributes($html);
	
	$invalid_tags = array(
		"img"	=> "",
		"input"	=> "",
		"br"	=> ", "
	);
	foreach ($invalid_tags as $tagname=>$tagreplace){
		$rgx = '/<'.$tagname.'\s*.*?\/*>/';
		$html = preg_replace($rgx,$tagreplace,$html);
	}
	
	$fn=getFromInput("fn");
	$convertUtf=getFromInput("utf");
	
	if ($convertUtf>0) {
		//$html = mb_convert_encoding($html,"UTF-7");
	}
	
	$html = html_entity_decode($html);
	
	$html = preg_replace('/<br[\s\/]*>/',", ",$html);
	$encoding = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8;" />';
	file_put_contents($filename,"$encoding<table>".$html."</table>");
	echo "$filename";
	if($fn){
		echo "{!}$fn";
	}
	die();
	$html = html_entity_decode($html,null);
	$tmp = preg_split('/<tr[^>]*>\t*\s*\r*\n*(.*?)<\/tr>/is',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
	
	$out = "";
	foreach ($tmp as $row){
		$row = trim($row);
		if(preg_match('/^<td/',$row)){
			$cells = preg_split('/\s*\t*\r*\n*<td[^>]*>(.*?)<\/td>\s*\t*\r*\n*/',$row,-1,PREG_SPLIT_DELIM_CAPTURE);
			//$out = var_export($cells,true);
			$roww = "";
			foreach ($cells as $cell){
				//$cell = trim($cell);
				if(strlen($cell)>0){
					$empty=false;
					//$cell = preg_replace('/<br\s*\/*>/i',"\r\n",$cell);
					$cell = preg_replace('/<br\s*\/*>/i',", ",$cell);
					//$cell = preg_replace('/<br\s*\/*>/i',chr(28),$cell);
					//$cell = preg_replace('/<\/*[a-zA-Z]+[^>]*\/*>/i',"",$cell);
					$cell = preg_replace('/onclick=".*?"/',"",$cell);
					$cell = preg_replace('/<.*?>/',"",$cell);
					$roww .= trim($cell)."\t";
				}
				//$out = substr($out,strlen($out)-1);
			}
			if (strlen(trim($roww))>0) {
				$out .= "$roww\r\n";
			}
			//$out .= var_export($cells,true);
		}
	}
	$out = substr($out,0,strlen($out)-2);
	file_put_contents($filename,$out);
	
	echo $filename;
	if($fn){
		echo "{!}$fn";
	}
}

function ulazUgovoraExport(){
	$html = readValFromInput("html");
	$html = html_entity_decode($html);
	$filename = "tmp".$_COOKIE["ypu"];
	
	$html = preg_split('/.*<tr.*?>/',$html);
	array_removeEmptyItems($html);
	
	$out = "";
	
	$head = array_splice($html,0,1);
	$head = trim($head[0]);
	$head = preg_replace('/>\n\t*</',"><",$head);
	$head = preg_replace('/<\/td><td\s*(.*?)>/',"\t",$head);
	$head = preg_replace('/<td\s*(.*?)>|<\/td>/',"",$head);
	$head = preg_replace('/<img\s*(.*?)\/*>/',"napomena",$head);
	//$head = nasaslova($head);
	
	$out .= $head;
	array_removeEmptyItems($html);
	foreach ($html as $row){
		$row = trim($row);
		if(stripos($row,"<td")>=0){
			$tmp = preg_split('/\s*<td.*?>/',$row);
			unset($tmp[0]);
			$r = "";
			$len = count($tmp);
			foreach ($tmp as $k=>$cell){
				$cell = preg_replace('/<img.*?\/*>/',"",$cell);
				if(($k+1)<$len){
					$cell = preg_replace('/<a.*?>.*?<\/a>/',"",$cell);
				}else{
					$cell = preg_replace('/<a.*?>.*?<\/a>/',"jeste",$cell);
				}
				
				$cell = preg_replace('/<\/td>/',"",$cell);
				$cell = preg_replace('/<\/*strong>/',"",$cell);
				$cell = preg_replace('/<\/li><li>/',"-",$cell);
				$cell = preg_replace('/<li>|<\/li>/',"",$cell);
				$cell = preg_replace('/<\/ul><ul.*?>/',", ",$cell);
				$cell = preg_replace('/<ul.*?>|<\/ul>/',"",$cell);
				$cell = preg_replace('/<br\s*\/*>/'," ",$cell);
				$cell = preg_replace('/(\r*\n)+/'," ",$cell);
				$cell = preg_replace('/<\/tr>|<\/tbody>/',"",$cell);
				$cell = preg_replace('/<input.*?>/','nije',$cell);
				$r .= "\t$cell";
			}
			$r = substr($r,1);
		}
		$out .= "\r\n".$r;
	}
	$out =trim($out,1);
	$out = nasaslova($out);
	//$out = preg_replace('/<br \/>|<br>/'," ",$out);
	
	file_put_contents($filename,$out);
	echo "$filename";
}

function menadzerReportExport(){
	$html = readValFromInput("html");
	$html = html_entity_decode($html);
	
	$filename = "tmp".$_COOKIE["ypu"];
	
	$html = preg_split('/.*<tr.*?>/',$html);
	array_removeEmptyItems($html);
	
	$out = "";
	
	$head = array_splice($html,0,1);
	$head = trim($head[0]);
	$head = preg_replace('/>\n\t*</',"><",$head);
	$head = preg_replace('/<\/td><td\s*(.*?)>/',"\t",$head);
	$head = preg_replace('/<td\s*(.*?)>|<\/td>/',"",$head);
	$head = preg_replace('/<img\s*(.*?)\/*>/',"napomena",$head);
	
	$out .= $head;
	
	array_removeEmptyItems($html);
	
	
	foreach ($html as $row){
		$row = trim($row);
		
		if(stripos($row,"<td")>=0){
			$tmp = preg_split('/\s*<td.*?>/',$row);
			unset($tmp[0]);
			$r = "";
			foreach ($tmp as $cell){
				if(!preg_match("/^</",$cell)){
					$cell = str_replace("</td>","",$cell);
					$cell = str_replace("</tr>","",$cell);
					$cell = str_replace("</tbody>","",$cell);
					$cell = preg_replace('/\r*\n/'," ",$cell);
					$r .= "\t".$cell;
				}
				
				if(preg_match('/^<a /',$cell)){
					$cell = stripslashes($cell);
					//echo $cell."\r\n";
					$tmp = preg_split('/onclick="alert\(\'(.*?)\'\)"/',$cell,-1,PREG_SPLIT_DELIM_CAPTURE);
					$napomena = $tmp[1];
					$r .= "\t".$napomena;
				}
			}
			$r = substr($r,1);
		}
		$out .= "\r\n".$r;
	}
	$out =trim($out,1);
	$out = preg_replace('/<br \/>|<br>/'," ",$out);
	
	file_put_contents($filename,$out);
	echo "$filename";
}

function listingDelatnostiXLS(){
	$html = readValFromInput("html");
	
	$html = html_entity_decode($html);
	$filename = "tmp".$_COOKIE["ypu"];
	
	$html = preg_split('/.*<tr.*?>/',$html);
	array_removeEmptyItems($html);
	
	$out = "";
	
	$head = array_splice($html,0,1);
	$head = trim($head[0]);
	$head = preg_replace('/>\n\t*</',"><",$head);
	$head = preg_replace('/<\/td><td\s*(.*?)>/',"\t",$head);
	$head = preg_replace('/<td\s*(.*?)>|<\/td>/',"",$head);
	$head = preg_replace('/<img\s*(.*?)\/*>/',"napomena",$head);
	//$head = nasaslova($head);
	
	$out .= $head;
	array_removeEmptyItems($html);
	$content = "";
	foreach ($html as $row){
		$row = trim($row);
		if(stripos($row,"<td")>=0){
			$tmp = preg_split('/\s*<td.*?>/',$row);
			$r = "";
			foreach ($tmp as $cell){
				$cell = preg_replace('/<img.*?\/*>/',"",$cell);
				$cell = preg_replace('/<a.*?>.*?<\/a>/',"",$cell);
				$cell = preg_replace('/<\/td>/',"",$cell);
				$cell = preg_replace('/<\/*strong>/',"",$cell);
				$cell = preg_replace('/<\/li><li>/',"-",$cell);
				$cell = preg_replace('/<li>|<\/li>/',"",$cell);
				$cell = preg_replace('/<\/ul><ul.*?>/',", ",$cell);
				$cell = preg_replace('/<ul.*?>|<\/ul>/',"",$cell);
				$cell = preg_replace('/<br\s*\/*>/'," ",$cell);
				$cell = preg_replace('/(\r*\n)+/'," ",$cell);
				$cell = preg_replace('/<\/tr>|<\/tbody>/',"",$cell);
				$r .= "\t$cell";
			}
			$r = substr($r,1);
		}
		$out .= "\r\n".$r;
	}
	
	file_put_contents($filename,$out);
	echo $filename;
}

function listingDelatnostiTXT(){
	
}

function pretragaDelatnostiXLS(){
	$html = readValFromInput("html");
	$userid = security::getUserId();
	$tmpfilename = "tmp$userid";
	
	$tmp = preg_split('/<tr[^>]*>\t*\s*\r*\n*(.*?)<\/tr>/is',$html,-1,PREG_SPLIT_DELIM_CAPTURE);
	foreach ($tmp as $item){
		if (preg_match('/^[\s\t]*<td/',$item)) {
			$tmp1 = preg_split('/<td[^>]*>[\t\s\n\r]*(.*?)<\/td>/is',$item,-1,PREG_SPLIT_DELIM_CAPTURE);
			//$tmp1 = array_map("cleanSplit",$tmp1);
			array_removeEmptyItems($tmp1);
			foreach ($tmp1 as $k=>$item1){
				if (preg_match('/<([a-zA-Z]+[0-9]*)\s*[^>]*>/',$item1)) {
					$item1 = preg_replace('/<([a-zA-Z]+[0-9]*)\s[^>]*>/'," ",$item1);
					$item1 = preg_replace('/<\/([a-zA-Z]+[0-9]*)>/'," ",$item1);
					$item1 = preg_replace('/[\s\t\n]+/'," ",$item1);
					$item1 = trim($item1);
					$item1 = preg_replace('/<br\s*\/*>/',", ",$item1);
					$tmp1[$k] = $item1;
				}
			}
			$data[] = $tmp1;
		}
	}
	//$content = var_export($data,true);
	
	$content = "";
	foreach ($data as $row){
		$_row = "";
		foreach ($row as $cell){
			if ($cell == "&nbsp;") {
				$cell = " ";
			}
			$_row .= "\t$cell";
		}
		$_row = substr($_row,1);
		$content .= "\r\n$_row";
	}
	$content = substr($content,2);
	
	$fp = fopen($tmpfilename,"w");
	fwrite($fp,$content);
	fclose($fp);
	
	echo "$tmpfilename{!}export.txt";
}




/********************************************************/

?>